package Ep03_DoublePoint.Offer_18;

import Ep02_LinkedList.ListNode;

/**
 * @author Jimmy Zhan WORKSTATION
 * @since 2023/7/13 14:07
 * 说明：
 *      常规的双指针删除链表节点操作，使用dummyHead避免只有一个节点且只删除这个节点的情况，少些一些条件分支
 */

public class Mine {
    public ListNode deleteNode(ListNode head, int val) {
        if (head == null) return head;
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode pre = dummyHead;
        ListNode cur = pre.next;

        while(cur != null) {
            if (cur.val == val) { // 删除cur
                pre.next = cur.next;
                return dummyHead.next;
            }
            pre = cur;
            cur = cur.next;
        }
        return dummyHead.next;
    }
}
